package myContacts;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;


public class Contact implements Serializable, Cloneable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<Email> emailAddresses;
	private String id;
	private Name name;
	private Nickname nickname;
	private Occupation occupation;
	private List<PhoneNumber> phoneNumbers;
	private List<StructuredPostalAddress> structuredPostalAddresses;
	private String versionId;
	private Link contactPhotoLink;
	private Link editLink;
	private String etag;
	
	private ImageIcon photo;
	
	private boolean registered;
	
	public Contact() {
		super();
		emailAddresses = new ArrayList<Email>();
		id = "";
		name = new Name();
		nickname = new Nickname();
		occupation = new Occupation();
		phoneNumbers = new ArrayList<PhoneNumber>();
		structuredPostalAddresses = new ArrayList<StructuredPostalAddress>();
		versionId = "";
		contactPhotoLink = new Link();
		photo = null;
		editLink = new Link();
		etag = "";
		registered = false;
	}

	public List<Email> getEmailAddresses() {
		return emailAddresses;
	}

	public void setEmailAddresses(List<Email> emailAddresses) {
		this.emailAddresses = emailAddresses;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Name getName() {
		return name;
	}

	public void setName(Name name) {
		this.name = name;
	}

	public Nickname getNickname() {
		return nickname;
	}

	public void setNickname(Nickname nickname) {
		this.nickname = nickname;
	}

	public Occupation getOccupation() {
		return occupation;
	}

	public void setOccupation(Occupation occupation) {
		this.occupation = occupation;
	}

	public List<PhoneNumber> getPhoneNumbers() {
		return phoneNumbers;
	}

	public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
		this.phoneNumbers = phoneNumbers;
	}

	public List<StructuredPostalAddress> getStructuredPostalAddresses() {
		return structuredPostalAddresses;
	}

	public void setStructuredPostalAddresses(
			List<StructuredPostalAddress> structuredPostalAddresses) {
		this.structuredPostalAddresses = structuredPostalAddresses;
	}

	public String getVersionId() {
		return versionId;
	}

	public void setVersionId(String versionId) {
		this.versionId = versionId;
	}

	public Link getContactPhotoLink() {
		return contactPhotoLink;
	}

	public void setContactPhotoLink(Link contactPhotoLink) {
		this.contactPhotoLink = contactPhotoLink;
	}

	public ImageIcon getPhoto() {
		return photo;
	
	}

	public void setPhoto(ImageIcon photo) {
		this.photo = photo;
	}

	public Link getEditLink() {
		return editLink;
	}

	public void setEditLink(Link editLink) {
		this.editLink = editLink;
	}

	public String getEtag() {
		return etag;
	}

	public void setEtag(String etag) {
		this.etag = etag;
	}
	
	public boolean isRegistered() {
		return registered;
	}

	public void setRegistered(boolean registered) {
		this.registered = registered;
	}

	@Override
	protected Contact clone() {
		// TODO Auto-generated method stub
		Contact contact = new Contact();
		
		for (Email email : emailAddresses) {
			
			contact.emailAddresses.add(email.clone());
		}		
		
		contact.id = id;
		contact.name = (name!=null) ? name.clone() : null;
		contact.nickname = (nickname!=null) ? nickname.clone() : null;
		contact.occupation = (occupation!=null) ? occupation.clone() : null;
		
		for (PhoneNumber phoneNumber : phoneNumbers) {
			
			contact.phoneNumbers.add(phoneNumber.clone());
		}
		
		for (StructuredPostalAddress postalAddress : structuredPostalAddresses) {
			
			contact.structuredPostalAddresses.add(postalAddress.clone());
		}
		
		contact.versionId = versionId;
		contact.contactPhotoLink = (contactPhotoLink!=null) ? contactPhotoLink.clone() : null;
		contact.photo = (photo!=null) ? new ImageIcon(photo.getImage()) : null;
		contact.editLink = (editLink!=null) ? editLink.clone() : null;
		contact.etag = etag;
		contact.registered = registered;
		
		return contact;
	}

	@Override
	public boolean equals(Object other) {
		
		if(other instanceof Contact)
		{
			return (id.equals( ((Contact)other).id ));
		}
		
		return false;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "Nome "+ name + " Photo "+photo;
	}
	
}
